package com.ws.client.controller;

import com.ws.client.feign.CodeService;
import com.ws.client.feign.SoapWSService;
import com.ws.client.feign.SoapWitherService;
import com.ws.client.model.*;
import com.ws.client.utils.XMLUtil;
import com.ws.client.webservice.CountryClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/client")
public class ClientController {


    @Autowired
    private CodeService codeService;

    @Autowired
    private SoapWSService soapWSService;
    @Autowired
    private SoapWitherService soapWitherService;

    @Autowired
    private CountryClient countryClient;


    //普通的feign http使用
    @GetMapping("/code")
    public String getCodeList(){
        return codeService.list();
    }


    //feign 调用soap方式
    @GetMapping("/soap-feign")
    public GetCountryResponse soapFeign(){
        GetCountryRequest request = new GetCountryRequest();
        request.setName("feign参数美国");
        return soapWSService.getCountry(request);
    }

    //spring webservice 调用soap方式
    @GetMapping("/soap-ws")
    public GetCountryResponse soapWS(){
        GetCountryResponse result = countryClient.getCountry("webservice参数马达加斯加");
        return result;
    }

    @GetMapping("/soap-feign-wither")
    public String soapFeignWither(){
        ValidateEmailAddress validateEmailAddress = new ValidateEmailAddress();
        validateEmailAddress.setTheEmail("2342@as.com");
        System.out.println(XMLUtil.toXML(validateEmailAddress));
        ValidateEmailAddress emailAddress = XMLUtil.fromXML(doGetXML(),ValidateEmailAddress.class);
        System.out.println(emailAddress.getTheEmail());
        ValidateEmailAddressResponse validateEmailAddressResponse = soapWitherService.getSupportCityResult(validateEmailAddress);
        return validateEmailAddressResponse.getValidateEmailAddressResult();
    }

    private String doGetXML(){
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<ValidateEmailAddress xmlns=\"http://WebXml.com.cn/\">\n" +
                "  <theEmail>2342@as.com</theEmail>\n" +
                "</ValidateEmailAddress>";
        return xml;
    }
}
